<?php
namespace app\api\controller;
use think\Controller;
use think\Cache;
use think\Validate;
use think\Db;
use think\Config;

class Member extends Controller{

  //会员卡信息页面
  public function membershipCardinfo(){
        if(cache('membership')){
            $card_info = cache('membership');
        }else{
            $card_info = db('membership_card')->field('card_id,type,name as card_name,img,price as card_price,des')
                                              ->where('state = 1')->order('sort desc,card_id desc')
                                              ->select();
            foreach($card_info as $key=>$value){
                if(!Validate::is($value['img'],'url')){
                    $card_info[$key]['img'] = 'https://'.Config::get('oss.bucket').'.'.Config::get('oss.endpoint').$value['img'];
                }
            }
        }
        if($card_info){
             cache('membership',$card_info,30);
             return json_encode(['code'=>200,'msg'=>'请求成功','membership_card_info'=>$card_info]);
        }else{
             return json_encode(['code'=>201,'msg'=>'请求数据为空']);
        }
  }

  //绑定会员(线下销售)  会员卡信息
  public function offlineSalesCardinfo(){
      $offline_id = input('post.offline_id');
      if(!is_numeric($offline_id)){return json_encode(['code'=>401,'msg'=>'offline_id类型错误']);}
      $card_order = db('card_order')->field('offline_id')->where("offline_id = {$offline_id}")->find();
      if($card_order){return json_encode(['code'=>401,'msg'=>'此二维码已被绑定']);}
      $offline_sale_info = db('card_offline')->field('card_id,create_time,effective,store_user_id')->where("offline_id = {$offline_id}")->find();
      if(($offline_sale_info['effective'] - (time() - $offline_sale_info['create_time'])/86400) <= 0){
         return json_encode(['code'=>401,'msg'=>'二维码已失效,不能绑定会员']);
      }
      $card_info = db('membership_card')->field('card_id,type,name as card_name,img,price as card_price,des')->where("card_id = {$offline_sale_info['card_id']} AND state = 1")->find();

      $card_info['store_user_sn'] = db('store_user')->where('store_user_id',$offline_sale_info['store_user_id'])->value('sn');
      if(!Validate::is($card_info['img'],'url')){
          $card_info['img'] = 'https://'.Config::get('oss.bucket').'.'.Config::get('oss.endpoint').$card_info['img'];
      }
      return json_encode(['code'=>200,'msg'=>'请求成功','card_info'=>$card_info]);
  }

  //是否是会员
  public function isMember(){
        $user_id = input('post.user_id');
        if(!is_numeric($user_id)){
            return json_encode(['code'=>401,'msg'=>'user_id类型错误']);
        }
        $whether_member = db('user')->where("user_id = {$user_id}")->value('whether_member');
        return json_encode(['code'=>200,'msg'=>'请求成功','whether_member'=>$whether_member]);
  }

  //购买会员卡   开通会员/续卡
   public function joinMembership(){
        $user_id = input('post.user_id');
        if(!is_numeric($user_id)){
            return json_encode(['code'=>401,'msg'=>'user_id类型错误']);
        }
        $user_info = db('user')->field('openid,whether_member')->where("user_id = {$user_id}")->find();
        $openid = $user_info['openid'];
        $card_id = input('post.card_id');
        if(!is_numeric($card_id)){
            return json_encode(['code'=>401,'msg'=>'card_id类型错误']);
        }
        $type = input('post.type');//0线上;1线下
        $card_order_sn = 'C'.generateOrderNum();
        $data = array(
            'card_order_sn' => $card_order_sn,
            'card_id' => $card_id,
            'user_id' => $user_id,
            'add_time' => time()
            );
        $card_info = db('membership_card')->field('type,price,give')->where("card_id = {$card_id}")->find();
        if($type == 0){
            $card_price = input('post.card_price');
            if($card_price == '' || $card_price == 'null' || $card_price == 'undefined'){
                return json_encode(['code'=>401,'msg'=>'card_price参数不能为空']);
            }
            $price = input('post.price');
            if($card_info['price'] != $card_price){
                return json_encode(['code'=>401,'msg'=>'price参数被篡改']);
            }
            $data['order_amount'] = $card_price;
            $data['pay_price'] = $data['order_amount'];
            $use_id = input('post.use_id');
            if($use_id){
              if(!is_numeric($use_id)){
                 return json_encode(['code'=>401,'msg'=>'use_id类型错误']);
               }
               $preferential_amount = input('post.preferential_amount');
               $data['use_id'] = $use_id;
               $coupons_id = db('coupons_use')->where("use_id = {$use_id}")->value('coupons_id'); 
               $coupons_info = db('coupons')->field('coupons_type,scope,conditions,discount,start_time,settlement_time')
                                            ->where("coupons_id = {$coupons_id}")
                                            ->find();
               if($coupons_info['scope'] != 0){return json_encode(['code'=>401,'msg'=>'优惠券使用范围scope出错']);}
               if($price < $coupons_info['conditions']){return json_encode(['code'=>401,'msg'=>'该优惠券未满足使用条件']);}
               if(time() < $coupons_info['start_time']){return json_encode(['code'=>401,'msg'=>'该优惠券还未生效']);}
               if(time() > $coupons_info['settlement_time']){return json_encode(['code'=>401,'msg'=>'该优惠券已失效']);} 
               if($coupons_info['coupons_type'] == 2){$coupons_info['discount'] = round($card_price * (1 - $coupons_info['discount']/100),2);}
               if($preferential_amount != $coupons_info['discount']){return json_encode(['code'=>401,'msg'=>'优惠金额错误','c'=>$coupons_info['discount']]);}
               $data['pay_price'] = ($card_price - $coupons_info['discount']) > 0 ?round(($card_price - $coupons_info['discount']),2):0;
            }
        }elseif($type == 1){
            $data['type'] = 1;
            $offline_id = input('post.offline_id');
            if(!is_numeric($offline_id)){return json_encode(['code'=>401,'msg'=>'offline_id类型错误']);}
            $data['offline_id'] = $offline_id;
            $data['card_status'] = 1;
            $data['pay_price'] = db('card_offline')->where('offline_id',$offline_id)->value('price');
            $data['order_amount'] = $data['pay_price'];
            $data['pay_time'] = time();
            //拿到佣金额度，和免押额度
            $card = db('membership_card')->field('one_commission,two_commission,exempted_quota')->where("card_id = '{$card_id}'")->find();
            $data['total_commission'] = $card['one_commission']+$card['two_commission'];
            $data['exempted_quota'] = $card['exempted_quota'];
        }
        //如果用户不是会员或者用户是会员但已过了有效期(会员卡有限期截止日期= 当前日期 + 会员卡时长（1月或1年）-1)
        if($user_info['whether_member'] == 0 || $user_info['whether_member'] == 2){
            $new_validity_time = date('Y-m-d',time());
            if($card_info['type'] == 0){
                $data['validity_time'] = strtotime("$new_validity_time+1year") - 1;
            }elseif($card_info['type'] == 1){
                $data['validity_time'] = strtotime("$new_validity_time+6month") - 1;
            }elseif($card_info['type'] == 2){
                $data['validity_time'] = strtotime("$new_validity_time+3month") - 1;
            }elseif($card_info['type'] == 3){
                $data['validity_time'] = strtotime("$new_validity_time+1month") - 1;
            }
        }elseif($user_info['whether_member'] == 1){
            //如果用户是会员，且该会员为过有效期
            $validity_time_array = db('user_card_log')->field('max(validity_time) as max_validity_time')->where("user_id = {$user_id}")->find(); 
            $latest_validity_time = $validity_time_array['max_validity_time'];
            $new_validity_time = date('Y-m-d',strtotime("$latest_validity_time+1day"));
            if($card_info['type'] == 0){
                $data['validity_time'] = strtotime("$new_validity_time+1year") - 1;
            }elseif($card_info['type'] == 1){
                $data['validity_time'] = strtotime("$new_validity_time+6month") - 1;
            }elseif($card_info['type'] == 2){
                $data['validity_time'] = strtotime("$new_validity_time+3month") - 1;
            }elseif($card_info['type'] == 3){
                $data['validity_time'] = strtotime("$new_validity_time+1month") - 1;
            }
        }
        if($card_info['give']){$data['validity_time'] = $data['validity_time'] + $card_info['give']*86400;}
        $store_user_sn = input('post.store_user_sn');
//        if($store_user_sn){
//            $storeuser['sn'] = $store_user_sn;
//            $userid['user_id'] = $store_user_sn;
//            $storeUser = db('store_user')->field('sn')->where($storeuser)->find();
//            $userId = db('user')->field('user_id')->where($userid)->find();
//            if($storeUser){
//                $data['channel'] = 1;
//            }elseif($userId){
//                $data['channel'] = 3;
//            }
//
//           $data['store_user_sn'] = $store_user_sn;
//           //$data['channel'] = 1;
//        }else{
//           $data['channel'] = 0;
//        }
       if($store_user_sn){
           $data['store_user_sn'] = $store_user_sn;
       }
       $data['channel'] = input('post.channel');



        // if($data['pay_price'] > 0){
        Db::startTrans();
        try{
           if($data['pay_price'] == 0){
                $data['card_status'] = 1;
                $data['pay_time'] = time();
           }
           $card_order_id = db('card_order')->insertGetId($data);
           if($type == 0){
               if($use_id){db('coupons_use')->where("use_id = {$use_id}")->update(['state'=>1]);}
               if($data['pay_price'] > 0){
                   // 实例支付接口
                   $pay = & load_wechat('Pay');
                   // 获取预支付ID
                   $notify_url = 'https://'.$_SERVER['HTTP_HOST'].'/api/Member/notify';
                   if($card_info['type'] == 0){
                       $goods_name = '购买共享珠宝会员卡年卡';
                   }elseif($card_info['type'] == 1){
                       $goods_name = '购买共享珠宝会员卡半年卡';
                   }elseif($card_info['type'] == 2){
                       $goods_name = '购买共享珠宝会员卡季卡';
                   }elseif($card_info['type'] == 3){
                       $goods_name = '购买共享珠宝会员卡月卡';
                   }
                   // $data['goods_price'] = 0.01;
                   $prepayid = $pay->getPrepayId($openid,$goods_name, $data['card_order_sn'], $data['pay_price'] * 100, $notify_url, $trade_type = "JSAPI");
                   // 创建JSAPI签名参数包
                   $options = $pay->createMchPay($prepayid);
               }elseif($data['pay_price'] == 0){
                   $logData = array(
                             'card_id' => $card_id,
                             'user_id' => $user_id,
                             'purchase_time' => date('Y-m-d',time())
                            );
                    $map = "user_id = {$user_id}";
                    $whether_member = db('user')->where($map)->value('whether_member');
                    if($whether_member == 0 || $whether_member == 2){
                         db('user')->where($map)->update(['whether_member'=>1,'validity_time'=>$data['validity_time']]);
                         $logData['start_time'] = date('Y-m-d',$data['add_time']);
                         $logData['validity_time'] = date('Y-m-d',$data['validity_time']);
                         //逾期后,续卡当天,当日逾期费用要减去
                         if($whether_member == 2){
                             $map .= " AND order_status > 4 AND type = 1";
                             $order_list = db('order')->field('order_id,overdue_number,overdue_price,overdue_time')->where($map)->select();
                             foreach($order_list as $key=>$value){
                                if((time() - $value['overdue_time']) < 86399){
                                     //计算每日的商品押金
                                     $goods_list = db('order_goods')->field('goods_number,rent')->where("order_id = {$value['order_id']}")->select();
                                     $rent = [];
                                     foreach($goods_list as $k=>$v){
                                        $rent[] = $goods_list[$k]['goods_number'] * $goods_list[$k]['rent'];
                                     }
                                     $rent_sum = array_sum($rent);
                                     $changeOverduePrice = [];
                                     $changeOverduePrice['overdue_price'] = $value['overdue_price'] - $rent_sum;
                                     $changeOverduePrice['overdue_number'] = $value['overdue_number'] - 1;
                                     db('order')->where("order_id = {$value['order_id']}")->update($changeOverduePrice);
                                }
                             }
                         }
                    }elseif($whether_member == 1){
                         $validity_time_array = db('user_card_log')->field('max(validity_time) as max_validity_time')->where($map)->find(); 
                         $latest_validity_time = $validity_time_array['max_validity_time'];
                         $new_validity_time = date('Y-m-d',strtotime("$latest_validity_time+1day"));
                         $logData['start_time'] = $new_validity_time;
                         $logData['validity_time'] = date('Y-m-d',$data['validity_time']);
                         db('user')->where($map)->update(['validity_time'=>$data['validity_time']]);
                    }
                    db('user_card_log')->insert($logData);
               }
           }elseif($type == 1){
               $logData = array(
                            'card_id' => $card_id,
                            'user_id' => $user_id,
                            'purchase_time' => date('Y-m-d',time()),
                            'total_commission'=>$card['one_commission']+$card['two_commission'],
                            'exempted_quota'=>$card['exempted_quota'],
                            'remarks'=>db('card_offline')->where('offline_id',$offline_id)->value('note'),
                            'money'=>$data['pay_price'],
                            );
              $map = "user_id = {$user_id}";
              $whether_member = db('user')->where($map)->value('whether_member');
              if($whether_member == 0 || $whether_member == 2){
                   db('user')->where($map)->update(['whether_member'=>1,'validity_time'=>$data['validity_time'],'quota'=>$card['exempted_quota'],'available_integral'=>$card['exempted_quota']]);
                   $logData['start_time'] = date('Y-m-d',$data['add_time']);
                   $logData['validity_time'] = date('Y-m-d',$data['validity_time']);
                   //逾期后,续卡当天,当日逾期费用要减去
                   if($whether_member == 2){
                       $map .= " AND order_status > 4 AND type = 1";
                       $order_list = db('order')->field('order_id,overdue_number,overdue_price,overdue_time')->where($map)->select();
                       foreach($order_list as $key=>$value){
                          if((time() - $value['overdue_time']) < 86399){
                               //计算每日的商品押金
                               $goods_list = db('order_goods')->field('goods_number,rent')->where("order_id = {$value['order_id']}")->select();
                               $rent = [];
                               foreach($goods_list as $k=>$v){
                                  $rent[] = $goods_list[$k]['goods_number'] * $goods_list[$k]['rent'];
                               }
                               $rent_sum = array_sum($rent);
                               $changeOverduePrice = [];
                               $changeOverduePrice['overdue_price'] = $value['overdue_price'] - $rent_sum;
                               $changeOverduePrice['overdue_number'] = $value['overdue_number'] - 1;
                               db('order')->where("order_id = {$value['order_id']}")->update($changeOverduePrice);
                          }
                       }
                   }

                  $jdinfoRecord = ['user_id' => $user_id, 'quota' => $card['exempted_quota'],'validity_time'=>$data['validity_time'],'source'=>1,'create_time'=>time(),'add_time'=>$data['add_time'],'it_available'=>1];
                  db('user_jdinfo_record')->insert($jdinfoRecord);
              }elseif($whether_member == 1){
                   $validity_time_array = db('user_card_log')->field('max(validity_time) as max_validity_time')->where($map)->find(); 
                   $latest_validity_time = $validity_time_array['max_validity_time'];
                   $new_validity_time = date('Y-m-d',strtotime("$latest_validity_time+1day"));
                   $logData['start_time'] = $new_validity_time;
                   $logData['validity_time'] = date('Y-m-d',$data['validity_time']);
                   db('user')->where($map)->update(['validity_time'=>$data['validity_time']]);

                  $jdinfoRecord = ['user_id' => $user_id, 'quota' => $card['exempted_quota'],'validity_time'=>$data['validity_time'],'source'=>1,'create_time'=>time(),'add_time'=>strtotime($new_validity_time)];
                  db('user_jdinfo_record')->insert($jdinfoRecord);
              }
              $logData['type'] = 1;
              $logData['offline_id'] = $offline_id;
              db('user_card_log')->insert($logData);
           }
           Db::commit();
        }catch(\Exception $e){
           Db::rollback();
           if($type == 0){$msg = '订单提交失败';}elseif($type == 1){$msg = '绑定失败';}
           return json_encode(['code' => 201,'msg' => $msg]);
        }
        if($type == 0){
            if($data['pay_price'] > 0){
               return json_encode(['code' => 200,'msg' => '提交成功','pay' => $options,'card_order_id' => $card_order_id]);
            }else{
               return json_encode(['code' => 200,'msg' => '提交成功','card_order_id' => $card_order_id]);
            }
        }elseif($type == 1){
            return json_encode(['code' => 200,'msg' => '绑定成功','card_order_id' => $card_order_id]);
        }  
   }

   //回调
   public function notify(){
            // 实例支付接口
            $pay = & load_wechat('Pay');
            // 获取支付通知
            $notifyInfo = $pay->getNotify();
            // 支付通知数据获取失败
            if($notifyInfo===FALSE){
                // 接口失败的处理
                echo $pay->errMsg;
            }else{
                //支付通知数据获取成功
                 if ($notifyInfo['result_code'] == 'SUCCESS' && $notifyInfo['return_code'] == 'SUCCESS') {
                    $order = db('card_order')->where("card_order_sn = '{$notifyInfo['out_trade_no']}'")->find();
                    if($order['card_status'] > 0){
                        return FALSE;
                    }
                    //拿到佣金额度，和免押额度
                     $card = db('membership_card')->field('one_commission,two_commission,exempted_quota')->where("card_id = '{$order['card_id']}'")->find();
                    Db::startTrans();
                    try{
                        //佣金
                        $userUserid = db('user')->field('leader,upper_level_id')->where("user_id = '{$order['user_id']}'")->find();
                        if(!empty($userUserid['leader'])){
                            if(!empty($userUserid['upper_level_id'])){
                                db('user')->where('user_id', $userUserid['upper_level_id'])->setInc('distribut_money',$card['two_commission']);
                                $comm['upper_level_id'] = $userUserid['upper_level_id'];
                                $comm['upper_level_commission'] = $card['two_commission'];
                            }
                            $comm['suser_id'] = $order['user_id'];$comm['user_id'] = $userUserid['leader'];$comm['order_id'] = $order['card_order_id'];$comm['type'] = 2;$comm['commission'] = $card['one_commission'];$comm['add_time'] = time();
                            db('commissions')->insert($comm);
                            db('user')->where('user_id', $userUserid['leader'])->setInc('distribut_money',$card['one_commission']);
                        }


                        $data = [];
                        $data['card_status'] = 1;
                        $data['pay_time'] = time();
                        $data['total_commission'] = $card['one_commission']+$card['two_commission'];
                        $data['exempted_quota'] = $card['exempted_quota'];
                        db('card_order')->where("card_order_sn = '{$notifyInfo['out_trade_no']}'")->update($data);
                        $logData = array(
                             'card_id' => $order['card_id'],
                             'user_id' => $order['user_id'],
                             'purchase_time' => date('Y-m-d',time()),
                             'money' => $order['pay_price'],
                            'total_commission'=>$card['one_commission']+$card['two_commission'],
                            'exempted_quota'=>$card['exempted_quota']
                            );
                        if($order['use_id'] != 0){$logData['use_id'] = $order['use_id'];}
                        $map = "user_id = {$order['user_id']}";
                        $whether_member = db('user')->where($map)->value('whether_member');
                        if($whether_member == 0 || $whether_member == 2){
                            //用户额度加
                             db('user')->where($map)->update(['whether_member'=>1,'validity_time'=>$order['validity_time'],'quota'=>$card['exempted_quota'],'available_integral'=>$card['exempted_quota']]);
                             $logData['start_time'] = date('Y-m-d',$order['add_time']);
                             $logData['validity_time'] = date('Y-m-d',$order['validity_time']);
                             //逾期后,续卡当天,当日逾期费用要减去
                             if($whether_member == 2){
                                 $map .= " AND order_status > 4 AND type = 1";
                                 $order_list = db('order')->field('order_id,overdue_number,overdue_price,overdue_time')->where($map)->select();
                                 foreach($order_list as $key=>$value){
                                    if((time() - $value['overdue_time']) < 86399){
                                         //计算每日的商品押金
                                         $goods_list = db('order_goods')->field('goods_number,rent')->where("order_id = {$value['order_id']}")->select();
                                         $rent = [];
                                         foreach($goods_list as $k=>$v){
                                            $rent[] = $goods_list[$k]['goods_number'] * $goods_list[$k]['rent'];
                                         }
                                         $rent_sum = array_sum($rent);
                                         $changeOverduePrice = [];
                                         $changeOverduePrice['overdue_price'] = $value['overdue_price'] - $rent_sum;
                                         $changeOverduePrice['overdue_number'] = $value['overdue_number'] - 1;
                                         db('order')->where("order_id = {$value['order_id']}")->update($changeOverduePrice);
                                    }
                                 }
                             }
                            $jdinfoRecord = ['user_id' => $order['user_id'], 'quota' => $card['exempted_quota'],'validity_time'=>$order['validity_time'],'source'=>1,'create_time'=>time(),'add_time'=>$order['add_time'],'it_available'=>1];
                            db('user_jdinfo_record')->insert($jdinfoRecord);
                        }elseif($whether_member == 1){
                             $validity_time_array = db('user_card_log')->field('max(validity_time) as max_validity_time')->where($map)->find(); 
                             $latest_validity_time = $validity_time_array['max_validity_time'];
                             $new_validity_time = date('Y-m-d',strtotime("$latest_validity_time+1day"));
                             $logData['start_time'] = $new_validity_time;
                             $logData['validity_time'] = date('Y-m-d',$order['validity_time']);
                             db('user')->where($map)->update(['validity_time'=>$order['validity_time']]);

                            $jdinfoRecord = ['user_id' => $order['user_id'], 'quota' => $card['exempted_quota'],'validity_time'=>$order['validity_time'],'source'=>1,'create_time'=>time(),'add_time'=>strtotime($new_validity_time)];
                            db('user_jdinfo_record')->insert($jdinfoRecord);
                        }
                        db('user_card_log')->insert($logData);

                        $orderData['order_id'] = $order['card_order_id'];
                        $orderData['type'] = 3;
                        $orderData['pay_code'] = $notifyInfo['transaction_id'];
                        $orderData['pay_price'] = $notifyInfo['total_fee']/100;
                        $orderData['time'] = strtotime($notifyInfo['time_end']);
                        $orderData['notify_info'] = json_encode($notifyInfo);
                        db('order_pay')->insert($orderData);
                        Db::commit();
                        $pay->replyXml(['return_code' => 'SUCCESS', 'return_msg' => 'DEAL WITH SUCCESS']);
                     } catch (\Exception $e) {
                        Db::rollback();
                     }
                }
            }
   }

   //我是会员(详情页)
   public function myMemberInfo(){
      $user_id = input('post.user_id');
      if(!is_numeric($user_id)){
         return json_encode(['code'=>401,'msg'=>'user_id类型错误']);
      }
      $map = "user_id = {$user_id}";
      $whether_member = db('user')->where($map)->value('whether_member');
      if($whether_member == 0){
         return json_encode(['code'=>401,'msg'=>'您还未开通会员,请跳转至购买会员卡页面']);
      }
      $page = input('post.page');
      if($whether_member == 1 || $whether_member == 2){
              $purchaseCardRecordCacheName = 'user_id'.$user_id.'purchaseCardRecord';
              if(cache($purchaseCardRecordCacheName)){
                 $purchaseCardRecord = cache($purchaseCardRecordCacheName);
              }else{
                 $purchaseCardRecord = db('user_card_log')->field('card_id,purchase_time,start_time,validity_time,money')
                                                          ->where($map)
                                                          ->order('purchase_time desc,card_log_id desc')
                                                          ->select();
                  foreach($purchaseCardRecord as $key=>$value){
                         $card_info = db('membership_card')->field('imgs,type as card_type')
                                                           ->where("card_id = {$value['card_id']}")
                                                           ->find();
                         $purchaseCardRecord[$key]['imgs'] = 'https://'.Config::get('oss.bucket').'.'.Config::get('oss.endpoint').$card_info['imgs'];
                         $purchaseCardRecord[$key]['card_type'] = $card_info['card_type'];
                         if(time() > (strtotime($value['validity_time']) + 86399)){
                            $purchaseCardRecord[$key]['is_effective'] = 2;
                         }else{
                            $purchaseCardRecord[$key]['is_effective'] = 1;
                         }
                  }
                  cache($purchaseCardRecordCacheName,$purchaseCardRecord,30);
              }
              $focusMapInfoCacheName = 'user_id'.$user_id.'focusMapInfo';
              if(cache('focusMapInfo')){
                 $focusMapInfo = cache($focusMapInfoCacheName);
              }else{
                  $focusMapInfo = $purchaseCardRecord[0];
                  unset($focusMapInfo['imgs']);
                  $focus_card_info = db('membership_card')->field('name,img')->where("card_id = {$focusMapInfo['card_id']}")->find();
                  $focusMapInfo['card_name'] = $focus_card_info['name'];
                  $focusMapInfo['card_img'] = 'https://'.Config::get('oss.bucket').'.'.Config::get('oss.endpoint').$focus_card_info['img'];
                  $focusMapInfo['card_validity_time'] = $focusMapInfo['validity_time'];
                  unset($focusMapInfo['validity_time']);
                  cache($focusMapInfoCacheName,$focusMapInfo,30);
              }
//              if(cache('goodsList')){
//                 $goodsList = cache('goodsList');
//              }else{
                    if(cache('user_quota')){
                        $user_quota = cache('user_quota');
                    }else{
                        $user_quota = db('user')->where($map)->value('quota');
                        cache('user_quota',$user_quota,300);
                    }
                    $spec_goods_price = db('spec_goods_price')->where('deposit','elt',$user_quota)->column('goods_id');

                  $goodswhere['sale_type'] = 2;
                  $goodswhere['shelves'] = 1;
                  $goodswhere['goods_id'] = array('in',$spec_goods_price);
                  $goodsList = db('goods')->field('goods_id,name as goods_name,small as goods_price,thumbnail_img')
                                          ->where($goodswhere)
                                          ->order('sort desc,goods_id')->paginate(6)->toArray();
                  foreach($goodsList['data'] as $k=>$v){
                    if(!Validate::is($v['thumbnail_img'],'url')){
                        $goodsList['data'][$k]['thumbnail_img'] = 'https://'.Config::get('oss.bucket').'.'.Config::get('oss.endpoint').$v['thumbnail_img'];
                    }
                  }
//                  cache('goodsList',$goodsList,30);
//              }
              return json_encode(['code'=>200,'msg'=>'请求成功','focusMapInfo'=>$focusMapInfo,'purchaseCardRecord'=>$purchaseCardRecord,'goodsList'=>$goodsList]);
      }
   }
}